package com.xiaoshuidi.cloud.module.member.xxljob;

import cn.hutool.core.date.DateUtil;
import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.module.member.enums.coupon.CouponStatusEnum;
import com.xiaoshuidi.cloud.module.member.enums.coupon.CouponTypeEnum;
import com.xiaoshuidi.cloud.module.member.pojo.coupon.CouponEntity;
import com.xiaoshuidi.cloud.module.member.repository.MemberCouponRepository;
import com.xiaoshuidi.cloud.module.member.util.CustomDateUtil;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
@RequiredArgsConstructor
public class MemberCouponReviewStatusJob {
    private final MemberCouponRepository memberCouponRepository;


    @TenantIgnore
    @XxlJob("member-scanner-coupon-status-job")
    @Transactional
    public void execute() {
        Date now = new Date();
        log.info("member跑优惠券活动开始结束状态定时任务开始" + now);
        //查询优惠券活动中状态为未开始状态和正在进行中状态
        List<CouponEntity> list = memberCouponRepository.getNotStartAndInProgressList();
        if (list != null && list.size() > 0) {
            //分出未开始状态
            List<CouponEntity> notStartList = list.stream().filter(c -> c.getCouponStatus().intValue() == CouponStatusEnum.NOT_STARTED.getCode().intValue())
                    .collect(Collectors.toList());
            List<CouponEntity> result = new ArrayList<>();
            if (notStartList != null && notStartList.size() > 0) {
                for (CouponEntity coupon : notStartList) {
                    boolean between = CustomDateUtil.isDateBetween(coupon.getActivityStartTime(), coupon.getActivityEndTime(), now);
                    if (between) {
                        coupon.setCouponStatus(CouponStatusEnum.IN_PROGRESS.getCode());
                    } else {
                        if (DateUtil.compare(coupon.getActivityStartTime(), now) > 0) {
                            //如果当前时间在活动开始时间之前那么是未开始
                            coupon.setCouponStatus(CouponStatusEnum.NOT_STARTED.getCode());
                        }
                    }
                    result.add(coupon);
                }
            }

            //分出进行中状态
            List<CouponEntity> inProgressList = list.stream().filter(c -> c.getCouponStatus().intValue() == CouponStatusEnum.IN_PROGRESS.getCode().intValue())
                    .collect(Collectors.toList());
            if (inProgressList != null && inProgressList.size() > 0) {
                for (CouponEntity coupon : inProgressList) {
                    //如果内部员工券一直都在进行中，因为没有活动开始时间和结束时间 ignore
                    if (coupon.getCouponType().intValue() != CouponTypeEnum.INTERNAL_EMPLOYEE_DISCOUNT.getNum().intValue()) {
                        if (DateUtil.compare(now, coupon.getActivityEndTime()) > 0) {
                            //如果当前时间在活动结束时间之后
                            coupon.setCouponStatus(CouponStatusEnum.COMPLETED.getCode());
                            result.add(coupon);
                        }
                    }
                }
            }

            //如果result不为空大于0
            if (result != null && result.size() > 0) {
                memberCouponRepository.batchUpdateCoupon(result);
            }

        }
    }


}
